package top.foxhome.productimages.productimages;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.Iterator;

/**
 * 文件处理工具
 */
public class FileUtils {
    /**
     * 获取图片文件的宽高
     *
     * @param file 图片文件
     * @return 返回图片的宽高
     */
    public static ImageSize getImageWH(File file) {
        ImageSize imageSize = new ImageSize();
        try {
            ImageInputStream iis = ImageIO.createImageInputStream(file);
            Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
            if (readers.hasNext()) {
                ImageReader reader = readers.next();
                reader.setInput(iis);

                int width = reader.getWidth(0);
                int height = reader.getHeight(0);
                imageSize.setWidth(width);
                imageSize.setHeight(height);
                // 还可以获取其他元数据
                // IIOMetadata metadata = reader.getImageMetadata(0);
            }
            iis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imageSize;
    }

    public static String getDesktopPath() {
        String os = System.getProperty("os.name").toLowerCase();
        String path;

        if (os.contains("win")) {
            // Windows 系统
            path = System.getenv("USERPROFILE") + "\\Desktop";
        } else if (os.contains("mac")) {
            // macOS 系统
            path = System.getProperty("user.home") + "/Desktop";
        } else if (os.contains("nix") || os.contains("nux")) {
            // Linux/Unix 系统 (桌面目录可能有多种名称)
            path = System.getProperty("user.home") + "/Desktop";
            if (!new File(path).exists()) {
                path = System.getProperty("user.home") + "/Schreibtisch"; // 德语
            }
        } else {
            // 其他系统回退到用户目录
            path = System.getProperty("user.home");
        }

        return path;
    }

    public static boolean deleteFile(File file) {
        try {
            Path path = Paths.get(file.getAbsolutePath());
            if (Files.exists(path)) {
                Files.walk(path)
                        .sorted(Comparator.reverseOrder())  // 先删除子目录和文件
                        .forEach(p -> {
                            try {
                                Files.delete(p);
                                System.out.println("已删除: " + p);
                            } catch (IOException e) {
                                System.err.println("无法删除: " + p + " - " + e.getMessage());
                            }
                        });
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
